package cl.mt.util.jmesa;


import java.util.List;

import org.jmesa.core.CoreContext;
import org.jmesa.view.ViewUtils;
import org.jmesa.view.component.Row;
import org.jmesa.view.html.toolbar.AbstractItemRenderer;
import org.jmesa.view.html.toolbar.AbstractToolbar;
import org.jmesa.view.html.toolbar.ImageItem;
import org.jmesa.view.html.toolbar.ImageItemImpl;
import org.jmesa.view.html.toolbar.ToolbarItem;
import org.jmesa.view.html.toolbar.ToolbarItemRenderer;
import org.jmesa.view.html.toolbar.ToolbarItemType;

/**
 * @author gcolombo
 *
 */
public class CustomToolbar extends AbstractToolbar {
    //~ Methods ------------------------------------------------------------------------------------

    /**
     * Documentacion pendiente.
     *
     * @return (documentacion pendiente).
     */
    @Override
    public String render() {
    	
        Row row = getTable().getRow();
        List columns = row.getColumns();

        boolean filterable = ViewUtils.isFilterable(columns);
        
        if (filterable) {
        	//en Internet Explorer se ve mal esto
        	//addToolbarItem(customItemFilter());
        	addToolbarItem(ToolbarItemType.FILTER_ITEM);
        	addToolbarItem(ToolbarItemType.SEPARATOR);
        	//en Internet Explorer se ve mal esto
            //addToolbarItem(customItemClear());
            addToolbarItem(ToolbarItemType.CLEAR_ITEM);
            addToolbarItem(ToolbarItemType.SEPARATOR);
        }

        boolean exportable = ViewUtils.isExportable(getExportTypes());

        if (exportable) {
            addExportToolbarItems(getExportTypes());
            addToolbarItem(ToolbarItemType.SEPARATOR);
        }

        addToolbarItem(ToolbarItemType.PREV_PAGE_ITEM);
        addToolbarItem(ToolbarItemType.NEXT_PAGE_ITEM);
        

        
        return super.render();
    }
    
    private ImageItem customItemFilter() {
        ImageItemImpl item = new ImageItemImpl();
        item.setAlt("Buscar y Ordenar");

        ToolbarItemRenderer renderer = new CustomItemRenderer(item, getCoreContext());
        item.setToolbarItemRenderer(renderer);

        return item;
    }
    
    private ImageItem customItemClear() {
        ImageItemImpl item = new ImageItemImpl();
        item.setAlt("Limpiar");

        ToolbarItemRenderer renderer = new CustomItemRenderer(item, getCoreContext());
        item.setToolbarItemRenderer(renderer);

        return item;
    }

    private static class CustomItemRenderer extends AbstractItemRenderer {
        public CustomItemRenderer(ToolbarItem item, CoreContext coreContext) {
            setToolbarItem(item);
            setCoreContext(coreContext);
        }

        public String render() {
            ToolbarItem item = getToolbarItem();
            return item.disabled();
        }
    }
}